package springboot.test15.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;

//文件上传测试
@Slf4j
@Controller
public class formController {
    @GetMapping("/form_layouts")
    public String form_layouts(){
        return "/Form/form_layouts";
    }

    //MultipartFile自动封装上传来的文件
    @PostMapping("/upload")
    public String upload(@RequestParam("email") String email,
                         @RequestParam("password") String password,
                         @RequestPart("Image") MultipartFile image,
                         @RequestPart("Images") MultipartFile[] images) throws IOException {
        log.info("上传信息: email={}, password={}, Image={}, Images={}",
                email, password, image.getSize(), images.length);
        if(!image.isEmpty()){
            //保存到文件服务器 或OSS服务器
            String originalFilename = image.getOriginalFilename();
            image.transferTo(new File("D:\\"+originalFilename));
        }
        if(images.length>0){
            for (MultipartFile one : images){
                if(!one.isEmpty()){
                    String originalFilename = one.getOriginalFilename();
                    one.transferTo(new File("D:\\"+originalFilename));
                }
            }
        }
        return "main";
    }
}
